package com.ecarx.common;

import static com.jakewharton.rxbinding2.view.RxView.clicks;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import java.lang.reflect.ParameterizedType;
import java.util.concurrent.TimeUnit;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;

public abstract class BaseActivity<V extends ViewModel,T extends ViewDataBinding> extends AppCompatActivity {
    protected T binding;
    protected CompositeDisposable disposable;
    protected V mViewModel;
    protected int DEFAULT_VARIABLE = -1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        disposable = new CompositeDisposable();
        mViewModel = getViewModel();

        binding = DataBindingUtil.setContentView(this, getLayoutResId());
        binding.setLifecycleOwner(this);
        if(getViewModelId()!=DEFAULT_VARIABLE){
            binding.setVariable(getViewModelId(),mViewModel);
        }
        initView();
    }
    protected abstract @LayoutRes int getLayoutResId();

    protected abstract void initView();

    public void onClick(View view, Consumer consumer) {
        disposable.add(
                clicks(view).throttleFirst(1, TimeUnit.SECONDS).doAfterNext(o -> {

                }).subscribe(consumer));
    }

    @Override
    protected void onDestroy() {
        if (disposable != null) {
            disposable.dispose();
        }
//        BaseApplication.bApp.getActivityControl().removeActivity(this);
        super.onDestroy();
    }

    protected ViewModelProvider getAppViewModelProvider() {
        return ((BaseApplication) getApplicationContext()).getAppViewModelProvider();
    }


    protected  <V extends ViewModel> V getViewModel() {
        Class clazz = this.getClass();
        Class classViewModel = null;
        while ((clazz != null) && (clazz.getGenericSuperclass() != null)) {
            if (clazz.getGenericSuperclass() instanceof ParameterizedType) {
                classViewModel = (Class) ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments()[0];
                if (ViewModel.class.isAssignableFrom(classViewModel) && BaseFragment.class.equals(clazz.getSuperclass())) {
                    break;
                }
            }
            clazz = clazz.getSuperclass();
        }
        if (classViewModel == null) {
            return null;
        }
            ViewModelProvider viewModelProvider = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication()));
            return (V) viewModelProvider.get(classViewModel);
    }

    protected abstract int getViewModelId();
}
